package com.jiahe.blog.controller;

import com.jiahe.blog.model.dto.ArticleBackDTO;
import com.jiahe.blog.model.dto.PageDTO;
import com.jiahe.blog.model.response.BlogResponse;
import com.jiahe.blog.model.vo.ArticleVO;
import com.jiahe.blog.model.vo.ConditionVO;
import com.jiahe.blog.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Api(tags = "文章模块")
@RestController
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @ApiOperation(value = "添加或修改文章")
    @PostMapping("/admin/articles")
    public BlogResponse<Void> saveOrUpdateArticle(@Valid @RequestBody ArticleVO articleVO) {
        articleService.saveOrUpdateArticle(articleVO);
        return BlogResponse.success(null);
    }

    @ApiOperation(value = "查看后台文章")
    @GetMapping("/admin/articles")
    public BlogResponse<PageDTO<ArticleBackDTO>> listArticleBack(ConditionVO conditionVO) {
        PageDTO<ArticleBackDTO> backDTOPageDTO = articleService.listArticleBackDTO(conditionVO);
        return BlogResponse.success(backDTOPageDTO);
    }
}
